package edu.cuit.campus.web.controller;

import edu.cuit.campus.po.Admin;
import edu.cuit.campus.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/19 10:22
 */
@Controller
@RequestMapping("/admin")
@SessionAttributes(names = "admin", types = Admin.class)
public class AdminController {
    @Resource private AdminService service;

    @PostMapping("/login")
    public String login(@RequestParam(name = "username") String username,
                        @RequestParam(name = "password") String password, Model model){
        var admin = service.login(username, password);
        if(admin != null){
            //将admin存入session(@SessionAttributes(names = "admin", types = Admin.class))
            model.addAttribute("admin", admin);
            return "redirect:../index.jsp";
        }
        return "redirect:../login.jsp";
    }

    /**
     * @SessionAttribute: 从session作用域中获取共享属性值
     *  name/value: 共享属性名,默认为参数名
     *  required: 是否必须,默认为true
     * @param admin
     * @return
     */
    @RequestMapping(path = "/show")
    @ResponseBody
    public Admin show(@SessionAttribute(name = "admin") Admin admin){
        return admin;
    }
}
